#!/usr/bin/env python
# coding: utf-8

# In[2]:


#!pip install keras
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
import matplotlib.dates as mdates
import numpy as np # linear algebra
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
import math   
from datetime import datetime, date 
import warnings
warnings.filterwarnings('ignore')
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense

print("Setup Complete")


# In[3]:


dam = pd.read_csv("3Dam.csv", encoding= 'cp949')
dam.head()


# In[7]:


dam.columns = ['date', 'water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']
dam.head()


# In[8]:


dam['inflow'] = pd.to_numeric(dam['inflow'],errors='coerce')
dam['outflow'] = pd.to_numeric(dam['outflow'],errors='coerce')
dam['inflow2'] = pd.to_numeric(dam['inflow2'],errors='coerce')
dam['outflow2'] = pd.to_numeric(dam['outflow2'],errors='coerce')
dam['inflow3'] = pd.to_numeric(dam['inflow3'],errors='coerce')
dam['outflow3'] = pd.to_numeric(dam['outflow3'],errors='coerce')


# In[9]:


print(dam.isnull().sum())


# In[10]:


dam = dam.dropna(axis=0)


# In[11]:


print(dam.isnull().sum())


# In[12]:


dam.describe()


# In[13]:


dam=dam[dam['water_level']>100]
dam=dam[dam['rainfall']>=0]
dam=dam[dam['inflow']>=0]
dam=dam[dam['outflow']>=0]

dam=dam[dam['rainfall2']>=0]
dam=dam[dam['inflow2']>=0]
dam=dam[dam['outflow2']>=0]

dam=dam[dam['water_level3']>0]
dam=dam[dam['rainfall3']>=0]
dam=dam[dam['inflow3']>=0]
dam=dam[dam['outflow3']>=0]


# In[14]:


dam.describe()


# In[15]:


dam = dam.reset_index(drop=True)


# # Remove Outlier

# In[16]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[17]:


#remove outlier

Q1 = dam['outflow'].quantile(0.25)
Q3 = dam['outflow'].quantile(0.75)
IQR = Q3 - Q1    #IQR is interquartile range. 

filter = (dam['outflow'] >= Q1 - 1.5 * IQR) & (dam['outflow'] <= Q3 + 1.5 *IQR)
dam = dam.loc[filter]
print(dam['outflow'].describe())
print('\n')
print(dam['outflow'].describe())


# In[18]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[19]:


dam = dam.reset_index(drop=True)


# In[20]:


dam["date"] = pd.to_datetime(dam["date"])


# In[21]:


dam.index = dam["date"]


# In[22]:


dam.head()


# # Normalize

# In[27]:


dam


# In[28]:


features = dam[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[29]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[30]:


print(dam_scaled)


# In[31]:


dam['variation'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[32]:


dam['variation2'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[33]:


dam['variation3'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[34]:


df = dam['variation']
df


# In[35]:


ds_list = []

for i in range(1,len(df)):
    ds = df[i]-df[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[36]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[37]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[38]:


df2 = dam['variation2']
df2


# In[39]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[40]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[41]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[42]:


df3 = dam['variation3']
df3


# In[43]:


ds_list = []

for i in range(1,len(df)):
    ds = df3[i]-df3[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[44]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[45]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[46]:


dam = dam[1:]


# In[47]:


dam = dam.reset_index(drop=True)
print(dam)


# In[48]:


dam['ds_dt'] = ds_dt
dam['ds_dt2'] = ds_dt2
dam['ds_dt3'] = ds_dt3


# In[49]:


dam = dam.reset_index(drop=True)
print(dam)


# In[50]:


dam["date"] = pd.to_datetime(dam["date"])


# In[51]:


dam.index = dam["date"]


# # GRU model

# In[52]:


dam.describe()


# In[53]:


168304*0.8


# In[54]:


train=dam[:134643]
test=dam[134643:168304]


# In[55]:


def make_dataset(data, label, window_size=120):
    feature_list = []
    label_list = []
    for i in range(len(data) - window_size):
        feature_list.append(np.array(data.iloc[i:i+window_size]))
        label_list.append(np.array(label.iloc[i+window_size]))
    return np.array(feature_list), np.array(label_list)


# In[56]:


feature_cols = ['rainfall2', 'inflow2', 'outflow2', 'ds_dt2']
label_cols = ['water_level2']

train_feature = train[feature_cols]
train_label = train[label_cols]

test_feature = test[feature_cols]
test_label = test[label_cols]


# In[57]:


x_train, y_train = make_dataset(train_feature,train_label,120)


# In[58]:


x_test, y_test = make_dataset(test_feature,test_label,120)


# In[69]:


test_feature.shape


# In[70]:


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import GRU

tmodel_s2d = Sequential()
tmodel_s2d.add(GRU(128, 
               input_shape=(x_train.shape[1], x_train.shape[2]), 
               activation='relu', 
               return_sequences=True)) 
tmodel_s2d.add(GRU(64, 
               input_shape=(x_train.shape[1], x_train.shape[2]), 
               activation='relu',
               return_sequences=False))
tmodel_s2d.add(Dense(1))


# In[71]:


tmodel_s2d.compile(loss='mean_squared_error', optimizer='adam')


history = tmodel_s2d.fit(x_train, y_train, 
                    epochs=20, 
                    batch_size=64)


# In[75]:


#tmodel_s2d.save("tmodel_s2d.h5")


# In[59]:


from tensorflow.keras.models import load_model
tmodel_s2d = load_model('tmodel_s2d.h5')


# # TRAIN

# In[64]:


plt.figure(figsize=(10,8))
predict_train  = tmodel_s2d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,predict_train, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[65]:


import hydroeval as he
nse = he.evaluator(he.nse, predict_train, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_train, y)

print(nse, kge)


# In[66]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[67]:


print("MAE : " + str(mean_absolute_error(y, predict_train)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_train)))

print("MSE : " + str(mean_squared_error(y, predict_train)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_train)))

def rmsle(y, predict_train):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_train))))

print("RMSLE = ", rmsle(y, predict_train))

print("R2 : " + str(r2_score(y, predict_train)))


# In[68]:


train=dam[:134643]


# In[69]:


outputs_train2 = pd.DataFrame(y,columns = ['water level'])
outputs_train2['predicted water level'] = predict_train
print(outputs_train2)


# In[70]:


oo = dam[120:134643]


# In[71]:


Date=pd.DataFrame(oo.index)


# In[72]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[73]:


outputs_train3 = pd.concat([Date,outputs_train2],axis = 1)

outputs_train3


# In[74]:


outputs_train3.index = outputs_train3["date"]


# In[75]:


outputs_train3


# In[76]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[77]:


rev_normal_tr = pd.DataFrame(predict_train)
rev_normal_tr = rev_normal_tr.ewm(span=2*24).mean()


# In[78]:


outputs_train4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_train4['water level pre'] = rev_normal_tr
print(outputs_train4)


# In[79]:


outputs_train4 = pd.concat([Date,outputs_train4],axis = 1)

outputs_train4.index = outputs_train4["date"]


# In[80]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[81]:


plt.figure(figsize=(10,8))
predict_train  = tmodel_s2d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,rev_normal_tr, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[82]:


import hydroeval as he


# In[83]:


import hydroeval as he
nse = he.evaluator(he.nse, rev_normal_tr, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_tr, y)

print(nse, kge)


# In[84]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_tr)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_tr)))

print("MSE : " + str(mean_squared_error(y, rev_normal_tr)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_tr)))

print("R2 : " + str(r2_score(y, rev_normal_tr)))


# In[85]:


#outputs_train4.to_csv('s2t_train.csv')


# In[78]:


#dam.to_csv('dam_s2.csv')


# # TEST

# In[86]:


plt.figure(figsize=(10,8))
predict_test  = tmodel_s2d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,predict_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[87]:


import hydroeval as he
nse = he.evaluator(he.nse, predict_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_test, y)

print(nse, kge)


# In[88]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[89]:


print("MAE : " + str(mean_absolute_error(y, predict_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_test)))

print("MSE : " + str(mean_squared_error(y, predict_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_test)))

def rmsle(y, predict_test):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_test))))

print("RMSLE = ", rmsle(y, predict_test))

print("R2 : " + str(r2_score(y, predict_test)))


# In[90]:


outputs_test2 = pd.DataFrame(y,columns = ['water level'])
outputs_test2['predicted water level'] = predict_test
print(outputs_test2)


# In[91]:


134643+120


# In[92]:


oo = dam[134763:168304]
Date=pd.DataFrame(oo.index)


# In[93]:


Date=pd.DataFrame(oo.index)


# In[94]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[95]:


outputs_test2


# In[96]:


outputs_test3 = pd.concat([Date,outputs_test2],axis = 1)

outputs_test3


# In[97]:


outputs_test3.index = outputs_test3["date"]


# In[98]:


outputs_test3


# In[99]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[100]:


#outputs_test3.to_csv('s2_before_ma.csv')


# In[230]:


rev_normal_test = pd.DataFrame(predict_test)
rev_normal_test = rev_normal_test.ewm(span=2*24).mean()


# In[231]:


outputs_test4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_test4['water level pre'] = rev_normal_test
print(outputs_test4)


# In[232]:


outputs_test4 = pd.concat([Date,outputs_test4],axis = 1)

outputs_test4.index = outputs_test4["date"]


# In[233]:


outputs_test4


# In[234]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[235]:


plt.figure(figsize=(10,8))
predict_test  = tmodel_s2d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,rev_normal_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[236]:


import hydroeval as he


# In[237]:


nse = he.evaluator(he.nse, rev_normal_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_test, y)

print(nse, kge)


# In[238]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_test)))

print("MSE : " + str(mean_squared_error(y, rev_normal_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_test)))

print("R2 : " + str(r2_score(y, rev_normal_test)))


# In[197]:


#outputs_test4.to_csv('s2t.csv')


# # Scenario

# # Inflow

# In[64]:


first=dam[134643:168304]


# In[65]:


first


# In[66]:


first['outflow2'] = first['outflow2']*1.4


# In[67]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[68]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[69]:


first['variation2'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[70]:


df2 = first['variation2']
df2


# In[577]:


df2 = df2.reset_index(drop=True)


# In[578]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[579]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[580]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[581]:


first = first[1:]


# In[582]:


first = first.reset_index(drop=True)


# In[583]:


first['ds_dt2'] = ds_dt2


# In[584]:


first = first.reset_index(drop=True)


# In[585]:


orginal_out1= predict_test[1:]


# In[586]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[587]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[588]:


sin_sm2a = tmodel_s2d.predict(x_rcheck1)


# In[589]:


obs = dam.water_level2[134764:168304].to_numpy()


# In[590]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.8 outflow'] = sin_sm2a


# In[591]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[592]:


outputs_plot1


# In[593]:


oo = dam[134764:168304]


# In[594]:


Date=pd.DataFrame(oo.index)


# In[595]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[596]:


outputs_plot1.index = outputs_plot1["date"]


# In[597]:


outputs_plot1


# In[598]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[599]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=2*24).mean()


# In[600]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=2*24).mean()


# In[601]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.8 outflow'] = sin1_169
print(obs)


# In[602]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[603]:


outputs_test4.index = outputs_test4["date"]


# In[604]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam Control dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[605]:


#outputs_test4.to_csv('s2t_0.8.csv')


# # Inflow

# In[247]:


first=dam[134643:168304]


# In[248]:


first['inflow2'] = first['inflow2']*1.4


# In[249]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[250]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[251]:


first['variation2'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[252]:


df2 = first['variation2']
df2


# In[253]:


df2 = df2.reset_index(drop=True)


# In[254]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[255]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[256]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[257]:


first = first[1:]


# In[258]:


first = first.reset_index(drop=True)


# In[259]:


first['ds_dt2'] = ds_dt2


# In[260]:


first = first.reset_index(drop=True)


# In[261]:


orginal_out1= predict_test[1:]


# In[262]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[263]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[264]:


sin_sm2a = tmodel_s2d.predict(x_rcheck1)


# In[265]:


obs = dam.water_level2[134764:168304].to_numpy()


# In[266]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 1.4 Inflow'] = sin_sm2a


# In[267]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[268]:


outputs_plot1


# In[269]:


oo = dam[134764:168304]


# In[270]:


Date=pd.DataFrame(oo.index)


# In[271]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[272]:


outputs_plot1.index = outputs_plot1["date"]


# In[273]:


outputs_plot1


# In[274]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[275]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=2*24).mean()


# In[276]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=2*24).mean()


# In[277]:


obs['original prediction'] = original_pre_169
obs['Scenario : 1.4 Inflow'] = sin1_169
print(obs)


# In[278]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[279]:


outputs_test4.index = outputs_test4["date"]


# In[280]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[281]:


#outputs_test4.to_csv('s2t_1.4_inflow.csv')


# # Rainfall

# In[382]:


first=dam[134643:168304]


# In[383]:


first['rainfall2'] = first['rainfall2']*0.6


# In[384]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[385]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[386]:


first['variation2'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[387]:


df2 = first['variation2']
df2


# In[388]:


df2 = df2.reset_index(drop=True)


# In[389]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[390]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[391]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[392]:


first = first[1:]


# In[393]:


first = first.reset_index(drop=True)


# In[394]:


first['ds_dt2'] = ds_dt2


# In[395]:


first = first.reset_index(drop=True)


# In[396]:


orginal_out1= predict_test[1:]


# In[397]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[398]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[399]:


sin_sm2a = tmodel_s2d.predict(x_rcheck1)


# In[400]:


obs = dam.water_level2[134764:168304].to_numpy()


# In[401]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.6 Rainfall'] = sin_sm2a


# In[402]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[403]:


oo = dam[134764:168304]


# In[404]:


Date=pd.DataFrame(oo.index)


# In[405]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[406]:


outputs_plot1.index = outputs_plot1["date"]


# In[407]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[408]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=2*24).mean()


# In[409]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=2*24).mean()


# In[410]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.6 Rainfall'] = sin1_169
print(obs)


# In[411]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[412]:


outputs_test4.index = outputs_test4["date"]


# In[415]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[414]:


#outputs_test4.to_csv('s2t_0.6_rainfall.csv')


# In[ ]:




